package com.mz.ozy.mvp.model;

import android.app.Activity;
import android.content.Context;
import android.text.TextUtils;

import com.mz.ozy.bean.LoginBean;
import com.mz.ozy.constants.Constants;
import com.mz.ozy.net.RetrofitUtils;
import com.mz.ozy.utils.JsonUtils;
import com.mz.ozy.utils.MD5Util;

import java.util.HashMap;
import java.util.Map;

import io.reactivex.Observable;
import okhttp3.ResponseBody;

/**
 * Created by administrator on 2018/12/3.
 */
public class LoginModel {

    /**
     * 账号登录
     *
     * @param context
     * @param phone
     * @param type
     * @param password
     * @param deviceid
     * @return
     */
    public Observable<ResponseBody> onLogin(Context context, String phone, int type, String password, String deviceid) {
        Map<String, String> jsonObject = new HashMap<>();
        jsonObject.put("username", phone);
        if (type == LoginBean.LOGIN_TYPE_CODE) {
            jsonObject.put("code", password);
            return RetrofitUtils.getApiService(context).post(Constants.TransCode.TransCode10002, JsonUtils.requestJsonDate(jsonObject), null);
        } else {
            jsonObject.put("password", MD5Util.getMD5String(password));
            return RetrofitUtils.getApiService(context).post(Constants.TransCode.TransCode10004, JsonUtils.requestJsonDate(jsonObject), null);
        }
    }

    /**
     * 获得支付宝授权
     *
     * @param context
     * @return
     */
    public Observable<ResponseBody> onGetAlipayAuth(Context context) {
        HashMap<String, String> map = new HashMap<>();
        return RetrofitUtils.getApiService(context).post(Constants.TransCode.TransCode10062, JsonUtils.requestJsonDate(map), Constants.tokenId);
    }

    /**
     * 微信or支付宝  授权登录
     *
     * @param context
     * @param type      1-微信 2-支付宝
     * @param auth_code 支付宝获取会员信息字符串
     * @param openid    微信登录唯一标识
     * @return
     */
    public Observable<ResponseBody> onAuthLogin(Context context, int type, String auth_code, String openid) {
        HashMap<String, String> map = new HashMap<>();
        map.put("type", type + "");
        if (!TextUtils.isEmpty(auth_code)) {
            map.put("auth_code", auth_code);
        }
        if (!TextUtils.isEmpty(openid)) {
            map.put("openid", openid);
        }
        return RetrofitUtils.getApiService(context).post(Constants.TransCode.TransCode10063, JsonUtils.requestJsonDate(map), Constants.tokenId);
    }

    public Observable<ResponseBody> checkUpdateVersion(Activity activity) {
        Map<String, String> maps = new HashMap<>();

        return RetrofitUtils.getApiService(activity).post(Constants.TransCode.TransCode10046, JsonUtils.requestJsonDate(maps), Constants.tokenId);
    }

    public Observable<ResponseBody> checkUpNew(Context activity) {
        Map<String, String> maps = new HashMap<>();

        return RetrofitUtils.getApiService(activity).post(Constants.TransCode.TransCode10010, JsonUtils.requestJsonDate(maps), Constants.tokenId);
    }

}
